package sg.edu.nus.iss.vmcs.store;

import java.io.IOException;


import sg.edu.nus.iss.vmcs.system.DrinkPropertyLoader;
import sg.edu.nus.iss.vmcs.system.Environment;

public class DrinkStoreCreator extends StoreCreator {
	private DrinksStore dStore;
	private PropertyLoader drinksLoader;
	
	public DrinkStoreCreator(){
		PropertyLoader defaultLoader=new DrinkPropertyLoader(Environment.DRINK_PROP_FILE);
		this.drinksLoader=defaultLoader;
	}
	
	public DrinkStoreCreator(PropertyLoader cashLoader){
		this.drinksLoader=cashLoader;		
	}
	
	public DrinksStore createStore()
	{
		dStore=DrinksStore.getInstance();
		try {
			this.initializeDrinkStore();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dStore;
	}
	private void initializeDrinkStore() throws IOException {

		// get the drink file from the environment property file;
		int numOfItems = drinksLoader.getNumOfItems();
		dStore.setStoreSize(numOfItems);

		for (int i = 0; i < numOfItems; i++) {
            DrinksStoreItem item = (DrinksStoreItem) drinksLoader.getItem(i);
			StoreObject brand = item.getContent();
			StoreObject existingBrand = dStore.findObject(brand.getName());
			if (existingBrand != null) {
			    item.setContent(existingBrand);
			}
			dStore.addItem(i, item);
		}
	}
}
